﻿using SnmpForm.SnmpWrapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace SnmpForm.APConfig
{
    class GetUpdateSystemStatusTask
    {
        ucUpdateSystem _objForm = null;
        List<BusinessWrapper.Ap> _aps = null;
        public GetUpdateSystemStatusTask(ucUpdateSystem objForm, List<BusinessWrapper.Ap> aps)
        {
            _objForm = objForm;
            _aps = aps;
        }
        public GetUpdateSystemStatusTask(ucUpdateSystem objForm, BusinessWrapper.Ap ap)
        {
            _objForm = objForm;
            _aps = new List<BusinessWrapper.Ap>();
            _aps.Add(ap);
        }
        public void Run()
        {
            foreach (BusinessWrapper.Ap ap in _aps)
            {
                string strUpdateStatus = "";
                try
                {
                    strUpdateStatus = ap.ScanUpdateSystem();
                }
                catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strUpdateStatus = "获取状态失败，有可能是由于ap重启中";
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strUpdateStatus = "获取状态失败，有可能是由于ap重启中";
                }
                catch (ApSnmpException ex)
                {
                    strUpdateStatus = ex.Message;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strUpdateStatus = "获取状态失败，有可能是由于ap重启中";
                }

                _objForm.BeginInvoke(_objForm.updateSystemStatus, new Object[] {ap.GetInfo().id,  strUpdateStatus });
            }
        }
    }
}
